home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 January: Mac OS SDK / Dev.CD Jan 99 SDK1.toast / Development Kits / Installer SDK 1.2 / Upgrader 1.2.1 & Engines / Upgrader 1.2.1 / Plug-in Examples / Simple App Launcher Plug-in / Editor Sources / CSALEditorWindow.cp next >
Encoding:
Text File  |  1998-08-25  |  12.4 KB  |  386 lines  |  [TEXT/CWIE]

  1.  
  2. #include "CTextEditorWindow.h"
  3. #include "CFileRefEditorWindow.h"
  4. #include "CSALEditorWindow.h"
  5. #include "EditorUtilities.h"
  6.  
  7. // Dialog panes
  8. const ResType     kPanelTitleET                     = 'ptsE';
  9. const ResType     kBackgroundPicET                 = 'bpiE';
  10. const ResType     kFirstHelpPicET                 = 'pihE';
  11. const ResType     kEditMainTextB                     = 'emtB';
  12. const ResType     kEditHelpTextB                     = 'ethB';
  13. const ResType     kEmbedHelpTextRB                 = 'emhR';
  14. const ResType     kFileHelpTextRB                 = 'sfhR';
  15. const ResType     kRemoveB                         = 'rmvB';
  16. const ResType     kLeaveAppRunningCB                 = 'larC';
  17. const ResType     kEditAppFileRefB                 = 'eafB';
  18. const ResType     kEditDocFileRefB                 = 'edfB';
  19. const ResType     kShowLaunchCheckboxCB             = 'slcC';
  20. const ResType     kPreSelectLaunchCheckboxCB         = 'lcdC';
  21. const ResType     kLaunchCheckboxTitleET             = 'lctE';
  22.  
  23.  
  24. // ---------------------------------------------------------------------------
  25. //        • CEditorWindow
  26. // ---------------------------------------------------------------------------
  27. CEditorWindow::CEditorWindow( SInt16 inFileRefNum, SInt16 inPreferenceRsrcID, SInt16 inResListRsrcID )
  28.     : StDialogHandler(200, NULL), mFileRefNum(inFileRefNum), mPreferenceRsrcID(inPreferenceRsrcID), mResListRsrcID(inResListRsrcID), mFlags(0), mStringListRsrcID(0), mMainTextRsrcID(0), mHelpTextRsrcID(0), mAppFileRefRsrcID(0), mDocFileRefRsrcID(0)
  29.  
  30. {
  31.  
  32.     if( mPreferenceRsrcID == 0 ) {
  33.         SInt16** theIDHandle = (SInt16**) GetIndResource( 'paul', 1 );    //Get Resource ID for plugin preference
  34.         if (theIDHandle != NULL)
  35.             mPreferenceRsrcID = **theIDHandle;
  36.         else
  37.             mPreferenceRsrcID = 3500;
  38.     }
  39.  
  40. }
  41.  
  42. // ---------------------------------------------------------------------------
  43. //        • ~CEditorWindow
  44. // ---------------------------------------------------------------------------
  45. CEditorWindow::~CEditorWindow()
  46. {
  47. }
  48.  
  49.  
  50. // ---------------------------------------------------------------------------
  51. //        • DoEdit
  52. //
  53. //        Opens the editor window and handles all editing actions.  Returns
  54. //        a value indicating how the user finished the editing task
  55. //
  56. // ---------------------------------------------------------------------------
  57. SInt32    CEditorWindow::DoEdit()
  58. {
  59.     
  60.     SInt32            theResult = -1;
  61.     LWindow*        theDialog = GetDialog();
  62.  
  63.     this->LoadWindowFromFile();
  64.     
  65.     theDialog->Show();
  66.     
  67.     // Set active field
  68.     LEditField*    theField = (LEditField*) mDialog->FindPaneByID(kPanelTitleET);
  69.     SignalIf_(theField == nil);
  70.     mDialog->SetLatentSub(theField);
  71.  
  72.     
  73.     while (true) {
  74.         MessageT    hitMessage = DoDialog();
  75.         
  76.         if (hitMessage == msg_Cancel) {                                    // Cancel button
  77.             theResult = 1;
  78.             break;
  79.             
  80.         } else if (hitMessage == msg_OK) {                                // OK button
  81.             this->SaveWindowToFile();
  82.             theResult = 0;
  83.             break;
  84.             
  85.         } else if (hitMessage == kRemoveB) {                            // Remove button
  86.             theResult = 2;
  87.             break;
  88.         
  89.         } else if (hitMessage == kEditMainTextB) {                        // Edit main text
  90.                 DoEditText( mFileRefNum, mMainTextRsrcID  );
  91.                 
  92.         } else if (hitMessage == kEditHelpTextB) {                        // Edit help text
  93.         
  94.             if( mDialog->FindPaneByID(kEmbedHelpTextRB)->GetValue() )
  95.                 DoEditText( mFileRefNum, mHelpTextRsrcID  );
  96.             else
  97.                 DoEditFileRef( mFileRefNum, mHelpTextRsrcID );
  98.                 
  99.         } else if (hitMessage == kEditAppFileRefB) {                    // Edit application location
  100.             DoEditFileRef( mFileRefNum, mAppFileRefRsrcID );
  101.         
  102.         } else if (hitMessage == kEditDocFileRefB) {                    // Edit document location
  103.             DoEditFileRef( mFileRefNum, mDocFileRefRsrcID );
  104.                 
  105.         } else {                                                        // Idle time tasks
  106.         
  107.             if( mDialog->FindPaneByID(kEmbedHelpTextRB)->GetValue() )
  108.                 mDialog->FindPaneByID(kFirstHelpPicET)->Enable();
  109.             else
  110.                 mDialog->FindPaneByID(kFirstHelpPicET)->Disable();
  111.         
  112.             if( mDialog->FindPaneByID(kShowLaunchCheckboxCB)->GetValue() ) {
  113.                 mDialog->FindPaneByID(kPreSelectLaunchCheckboxCB)->Enable();
  114.                 mDialog->FindPaneByID(kLaunchCheckboxTitleET)->Enable();
  115.             } else {
  116.                 mDialog->FindPaneByID(kPreSelectLaunchCheckboxCB)->Disable();
  117.                 mDialog->FindPaneByID(kLaunchCheckboxTitleET)->Disable();
  118.             }
  119.  
  120.         }
  121.     }
  122.     
  123.     return theResult;
  124. }
  125.  
  126.  
  127. // ---------------------------------------------------------------------------
  128. //        • SaveWindowToFile
  129. //
  130. //        Writes the contents of the window out to the plug-in's preference
  131. //        resource.
  132. //
  133. // ---------------------------------------------------------------------------
  134. void CEditorWindow::SaveWindowToFile()
  135. {
  136.     SInt16    savedResFile = CurResFile();
  137.     UseResFile( mFileRefNum );
  138.     
  139.     TRsrcHandle* theRsrcStream = new TRsrcHandle();
  140.     SignalIf_(theRsrcStream == nil);
  141.     
  142.     if( mStringListRsrcID == 0 )
  143.          mStringListRsrcID = GetUniqueIDForResType( mFileRefNum, 'STR#' );         
  144.  
  145.     theRsrcStream->AppendShort( 3 );    // Append Format
  146.  
  147.     // Main Text flag
  148.     /*
  149.     LStdRadioButton* theRadioButton = (LStdRadioButton*) mDialog->FindPaneByID(kEmbedMainTextRB);
  150.     SignalIf_(theRadioButton == nil);
  151.     if( theRadioButton->GetValue() )
  152.         mFlags &= ~kMainTextInFile;
  153.     else
  154.         mFlags |= kMainTextInFile;
  155. */
  156.     // Help Text flag
  157.     LStdRadioButton* theRadioButton = (LStdRadioButton*) mDialog->FindPaneByID(kEmbedHelpTextRB);
  158.     SignalIf_(theRadioButton == nil);
  159.     if( theRadioButton->GetValue() )
  160.         mFlags &= ~kHelpTextInFile;
  161.     else
  162.         mFlags |= kHelpTextInFile;
  163.  
  164.  
  165.     // Leave app running flag
  166.     LStdCheckBox*    theCheckbox = (LStdCheckBox*) mDialog->FindPaneByID(kLeaveAppRunningCB);
  167.     SignalIf_(theCheckbox == nil);
  168.     if( theCheckbox->GetValue() )
  169.         mFlags |= kKeepApplicationOpen;
  170.     else
  171.         mFlags &= ~kKeepApplicationOpen;
  172.  
  173.     // Show Launch Checkbox
  174.     theCheckbox = (LStdCheckBox*) mDialog->FindPaneByID(kShowLaunchCheckboxCB);
  175.     SignalIf_(theCheckbox == nil);
  176.     if( theCheckbox->GetValue() )
  177.         mFlags |= kShowLaunchCheckbox;
  178.     else
  179.         mFlags &= ~kShowLaunchCheckbox;
  180.  
  181.     // Defalut checkbox state
  182.     theCheckbox = (LStdCheckBox*) mDialog->FindPaneByID(kPreSelectLaunchCheckboxCB);
  183.     SignalIf_(theCheckbox == nil);
  184.     if( theCheckbox->GetValue() )
  185.         mFlags |= kPrecheckLaunchCheckbox;
  186.     else
  187.         mFlags &= ~kPrecheckLaunchCheckbox;
  188.  
  189.  
  190.     theRsrcStream->AppendShort( mFlags );
  191.     theRsrcStream->AppendShort( mMainTextRsrcID );
  192.     theRsrcStream->AppendShort( mHelpTextRsrcID );
  193.  
  194.  
  195.     // First Help pict value
  196.     LEditField* theField = (LEditField*) mDialog->FindPaneByID(kFirstHelpPicET);
  197.     SignalIf_(theField == nil);
  198.     SInt16    theHelpFirstPictRsrcID = theField->GetValue();
  199.     theRsrcStream->AppendShort( theHelpFirstPictRsrcID );
  200.     
  201.     // Background pict value
  202.     theField = (LEditField*) mDialog->FindPaneByID(kBackgroundPicET);
  203.     SignalIf_(theField == nil);
  204.     SInt16     theBackgroundPictRsrcID = theField->GetValue();
  205.     theRsrcStream->AppendShort( theBackgroundPictRsrcID );
  206.     
  207.     // Panel name
  208.     theField = (LEditField*) mDialog->FindPaneByID(kPanelTitleET);
  209.     SignalIf_(theField == nil);
  210.     Str255    theStr;
  211.     theField->GetDescriptor( theStr );
  212.     WriteStringListResourceIndex( mFileRefNum, theStr, mStringListRsrcID, 1 );
  213.     theRsrcStream->AppendShort( mStringListRsrcID );
  214.             
  215.     // Help window title string
  216.     UseResFile( savedResFile );
  217.     GetIndString( theStr, 233, 2 );
  218.     UseResFile( mFileRefNum );
  219.     
  220.     WriteStringListResourceIndex( mFileRefNum, theStr, mStringListRsrcID, 2 );            
  221.     
  222.     // Launch Checkbox name
  223.     theField = (LEditField*) mDialog->FindPaneByID(kLaunchCheckboxTitleET);
  224.     SignalIf_(theField == nil);
  225.     theField->GetDescriptor( theStr );
  226.     WriteStringListResourceIndex( mFileRefNum, theStr, mStringListRsrcID, 3 );
  227.     
  228.             
  229.     // App File Ref
  230.     theRsrcStream->AppendShort( mAppFileRefRsrcID );
  231.     
  232.     // Doc File Ref
  233.     theRsrcStream->AppendShort( mDocFileRefRsrcID );
  234.  
  235.     
  236.     // Build Res list resource
  237.     ResetResListResource( mFileRefNum, mResListRsrcID );
  238.     AppendToResListResource( mFileRefNum, mResListRsrcID, 'slpr', mPreferenceRsrcID );
  239.     AppendToResListResource( mFileRefNum, mResListRsrcID, 'STR#', mStringListRsrcID );
  240.     
  241.     if( mFlags & kHelpTextInFile ) {
  242.         AppendToResListResource( mFileRefNum, mResListRsrcID, 'flrf', mHelpTextRsrcID );
  243.     } else {
  244.         AppendToResListResource( mFileRefNum, mResListRsrcID, 'TEXT', mHelpTextRsrcID );
  245.         AppendToResListResource( mFileRefNum, mResListRsrcID, 'styl', mHelpTextRsrcID );
  246.         AppendToResListResource( mFileRefNum, mResListRsrcID, 'PICT', theHelpFirstPictRsrcID );
  247.     }
  248.     
  249.     if( mFlags & kMainTextInFile ) {
  250.         AppendToResListResource( mFileRefNum, mResListRsrcID, 'flrf', mMainTextRsrcID );
  251.     } else {
  252.         AppendToResListResource( mFileRefNum, mResListRsrcID, 'TEXT', mMainTextRsrcID );
  253.         AppendToResListResource( mFileRefNum, mResListRsrcID, 'styl', mMainTextRsrcID );
  254.     }
  255.     AppendToResListResource( mFileRefNum, mResListRsrcID, 'PICT', theBackgroundPictRsrcID );
  256.     AppendToResListResource( mFileRefNum, mResListRsrcID, 'PICT', theBackgroundPictRsrcID  + 1 );
  257.  
  258.     AppendToResListResource( mFileRefNum, mResListRsrcID, 'flrf', mAppFileRefRsrcID );
  259.     AppendToResListResource( mFileRefNum, mResListRsrcID, 'flrf', mDocFileRefRsrcID );
  260.  
  261.     theRsrcStream->Write( mFileRefNum, 'slpr', mPreferenceRsrcID );
  262.  
  263.             
  264.     delete theRsrcStream;
  265.     
  266.     UseResFile( savedResFile );
  267. }
  268.  
  269.  
  270. // ---------------------------------------------------------------------------
  271. //        • LoadWindowFromFile
  272. //
  273. //        Reads plug-in's preference resource and fills in fields of editor window.
  274. //        If the resource hasn't been created yet, then routine provides default
  275. //        values for the fields.
  276. //
  277. // ---------------------------------------------------------------------------
  278. void CEditorWindow::LoadWindowFromFile()
  279. {
  280.     
  281.     SInt16    savedResFile = CurResFile();
  282.     UseResFile( mFileRefNum );
  283.     
  284.     Handle         theRsrcHandle = Get1Resource( 'slpr', mPreferenceRsrcID );
  285.     
  286.     if( theRsrcHandle )
  287.     {
  288.         TRsrcHandle* theRsrcStream = new TRsrcHandle( theRsrcHandle );
  289.         SignalIf_(theRsrcStream == nil);
  290.     
  291.         if( theRsrcStream->ReadShort() == 3)
  292.         {
  293.         
  294.             mFlags                             = theRsrcStream->ReadShort();
  295.  
  296.             // Set Embedded Main Text Radio Button
  297.             /*
  298.             LStdRadioButton* theRadioButton = (LStdRadioButton*) mDialog->FindPaneByID(kEmbedMainTextRB);
  299.             SignalIf_(theRadioButton == nil);
  300.             theRadioButton->SetValue( (mFlags & kMainTextInFile) != kMainTextInFile );
  301.  
  302.             // Set File Help Main Radio Button
  303.             theRadioButton = (LStdRadioButton*) mDialog->FindPaneByID(kFileMainTextRB);
  304.             SignalIf_(theRadioButton == nil);
  305.             theRadioButton->SetValue( (mFlags & kMainTextInFile) == kMainTextInFile );
  306.  
  307. */
  308.             // Set Embedded Help Text Radio Button
  309.             LStdRadioButton* theRadioButton = (LStdRadioButton*) mDialog->FindPaneByID(kEmbedHelpTextRB);
  310.             SignalIf_(theRadioButton == nil);
  311.             theRadioButton->SetValue( (mFlags & kHelpTextInFile) != kHelpTextInFile );
  312.  
  313.             // Set File Help Text Radio Button
  314.             theRadioButton = (LStdRadioButton*) mDialog->FindPaneByID(kFileHelpTextRB);
  315.             SignalIf_(theRadioButton == nil);
  316.             theRadioButton->SetValue( (mFlags & kHelpTextInFile) == kHelpTextInFile );
  317.  
  318.             mMainTextRsrcID                    = theRsrcStream->ReadShort();
  319.             mHelpTextRsrcID                    = theRsrcStream->ReadShort();
  320.  
  321.             // Set First Help pict value
  322.             LEditField* theField = (LEditField*) mDialog->FindPaneByID(kFirstHelpPicET);
  323.             SignalIf_(theField == nil);
  324.             theField->SetValue( theRsrcStream->ReadShort() );
  325.             
  326.             // Set Background pict value
  327.             theField = (LEditField*) mDialog->FindPaneByID(kBackgroundPicET);
  328.             SignalIf_(theField == nil);
  329.             theField->SetValue( theRsrcStream->ReadShort() );
  330.             
  331.             // Set Panel name
  332.             theField = (LEditField*) mDialog->FindPaneByID(kPanelTitleET);
  333.             SignalIf_(theField == nil);
  334.             mStringListRsrcID = theRsrcStream->ReadShort(); 
  335.             Str255    thePanelTitleStr;
  336.             GetIndString( thePanelTitleStr, mStringListRsrcID, 1 );
  337.             theField->SetDescriptor( thePanelTitleStr );
  338.  
  339.             // Set Launch Checkbox title
  340.             theField = (LEditField*) mDialog->FindPaneByID(kLaunchCheckboxTitleET);
  341.             SignalIf_(theField == nil);
  342.             Str255    theLaunchCheckboxTitleStr;
  343.             GetIndString( theLaunchCheckboxTitleStr, mStringListRsrcID, 3 );
  344.             theField->SetDescriptor( theLaunchCheckboxTitleStr );
  345.  
  346.  
  347.             // Leave app running flag
  348.             LStdCheckBox*    theCheckbox = (LStdCheckBox*) mDialog->FindPaneByID(kLeaveAppRunningCB);
  349.             SignalIf_(theCheckbox == nil);
  350.             theCheckbox->SetValue( (mFlags & kKeepApplicationOpen) == kKeepApplicationOpen );
  351.  
  352.             // Show Launch Checkbox
  353.             theCheckbox = (LStdCheckBox*) mDialog->FindPaneByID(kShowLaunchCheckboxCB);
  354.             SignalIf_(theCheckbox == nil);
  355.             theCheckbox->SetValue( (mFlags & kShowLaunchCheckbox) == kShowLaunchCheckbox );
  356.  
  357.             // Default Launch Checkbox state
  358.             theCheckbox = (LStdCheckBox*) mDialog->FindPaneByID(kPreSelectLaunchCheckboxCB);
  359.             SignalIf_(theCheckbox == nil);
  360.             theCheckbox->SetValue( (mFlags & kPrecheckLaunchCheckbox) == kPrecheckLaunchCheckbox );
  361.  
  362.             // App File Ref
  363.             mAppFileRefRsrcID = theRsrcStream->ReadShort();
  364.             
  365.             // Doc File Ref
  366.             mDocFileRefRsrcID = theRsrcStream->ReadShort();
  367.  
  368.         }
  369.         
  370.         delete theRsrcStream;
  371.  
  372.     } else {
  373.         mFlags                        = 0;
  374.         mStringListRsrcID            = 0;
  375.         mMainTextRsrcID                = 0;
  376.         mHelpTextRsrcID                = 0;
  377.         mAppFileRefRsrcID            = 0;
  378.         mDocFileRefRsrcID            = 0;
  379.     }
  380.     
  381.     UseResFile( savedResFile );
  382. }
  383.  
  384.  
  385.  
  386.